import { defineStore } from 'pinia';
export const useUserInfo = defineStore('userInfo', {
    state: () => {
        return {
            id: '',
			accountId:'',
            mobile: '',
            email: '',
            token: '',
            avatar: '',
			openid:'',
        }
    },
    actions: {
        /**
         * 状态批量填充
         * @param state 新状态数据
         */
        dataFill(state) {
			uni.setStorageSync('alisinceUser',JSON.stringify(state));
            this.$patch(state)
        },
        removeToken() {
           this.$state.token = ''
			uni.removeStorageSync('aliSinceToken');
			this.$state.accountId = '';
			uni.removeStorageSync('aliSinceAccountId');
			this.$reset()
        },
        setToken(token) {
            uni.setStorageSync('aliSinceToken',token);
            this.$state.token = token
        },
        getToken() {
			const BearToken = this.$state.token || uni.getStorageSync('aliSinceToken')
            return  BearToken
        },
		removeOpenid(){
			this.$state.openid = ''
			uni.removeStorageSync('openid')
		},
		setOpenid( openid ){
			uni.setStorageSync('openid',openid);
			this.$state.openid = openid
		},
		getOpenid(){
			return this.$state.openid || uni.getStorageSync('openid')
		},
		getAccountId(){
			const BearId = this.$state.accountId || uni.getStorageSync('aliSinceAccountId')
			return BearId
		},
		setAccountId(id){
			uni.setStorageSync('aliSinceAccountId',id);
			this.$state.accountId = id
		},
		getUserId(){
			const BearId = this.$state.id || uni.getStorageSync('aliSinceUserId')
			return BearId
		},
		setUserId(id){
			uni.setStorageSync('aliSinceUserId',id);
			this.$state.id = id
		},
        logout() {
            this.$state.token = ''
            uni.removeStorageSync('aliSinceToken');
            this.accountId = '';
            uni.removeStorageSync('aliSinceAccountId');
            this.$reset()
			uni.removeStorageSync('aliSinceUserId')
        },
        isLogin() {
            return this.getAccountId() && this.getToken()
        },
    },
    
})
